﻿using System;
using System.Collections.Generic;
using UnityEngine;
public class Actor : IInitByGameLevel, IDispose, ITick, ILogicUpdate
{
    private IGGameLevelMgr _cacheGameLevelMgr;
    public IGGameLevelMgr GameLevelMgr 
    {
        get 
        {
            return _cacheGameLevelMgr;
        }
    }

    private uint _ident;
    public uint Ident 
    {
        get 
        {
            return _ident;
        }
    }

    private string _name;
    public string Name 
    {
        get 
        {
            return _name;
        }
        set 
        {
            _name = value;
        }
    }

    //-------------------------Helper---------------------------------------------------------

    private Dictionary<EnumHelper, Helper> _dicOfHelpers = new Dictionary<EnumHelper, Helper>();
    private List<Helper> _listOfHelpers = new List<Helper>();

    public Helper GetHelper(EnumHelper eHelper) 
    {
        if (_dicOfHelpers.ContainsKey(eHelper) == false)
        {
            return null;
        }
        var result = _dicOfHelpers[eHelper];
        return result;
    }
    public void SetHelper(EnumHelper eHelper,Helper helper) 
    {
        if (_dicOfHelpers.ContainsKey(eHelper))
        {
            Debug.LogWarning($"[Actor] SetHelper 重复加载Helper :{eHelper} helper:{helper}");
            return;
        }
        _dicOfHelpers.Add(eHelper,helper);
        _listOfHelpers.Add(helper);
    }
    public bool RemoveHelper(EnumHelper ehelper) 
    {
        if (_dicOfHelpers.ContainsKey(ehelper) == false)
        {
            return false;
        }
        var helper = _dicOfHelpers[ehelper];
        _dicOfHelpers.Remove(ehelper);
        _listOfHelpers.Remove(helper);
        return false;
    }

    //--------------------Attribute---------------------------------------------------------------
    private Dictionary<EnumAttributeItem, Attribute> _dicOfAttributes = new Dictionary<EnumAttributeItem, Attribute>();
    private List<Attribute> _listOfAttributes = new List<Attribute>();

    public Attribute GetAttribute(EnumAttributeItem eAttribute)
    {
        if (_dicOfAttributes.ContainsKey(eAttribute) == false)
        {
            return null;
        }
        var result = _dicOfAttributes[eAttribute];
        return result;
    }
    public void SetAttribute(EnumAttributeItem eAttribute, Attribute attribute)
    {
        if (_dicOfAttributes.ContainsKey(eAttribute))
        {
            Debug.LogWarning($"[Actor] SetAttribute 重复加载attribute :{eAttribute} attribute:{attribute}");
            return;
        }
        _dicOfAttributes.Add(eAttribute, attribute);
        _listOfAttributes.Add(attribute);
    }
    public bool RemoveAttribute(EnumAttributeItem eAttribute)
    {
        if (_dicOfAttributes.ContainsKey(eAttribute) == false)
        {
            return false;
        }
        var attribute = _dicOfAttributes[eAttribute];
        _dicOfAttributes.Remove(eAttribute);
        _listOfAttributes.Remove(attribute);
        return false;
    }


    //-----------------------------------------------------------
    /*
     * Actor生命周期:
     * 1. ActorSpawn: 创建Actor
     * 2. ActorSetHelper: Actor工厂根据类型添加对应的Helper添加到Actor身上
     * 3. ActorInit: Actor工厂初始化Actor,Actor初始化Helper
     * 4. ActorDispose: Actor工厂销毁Actor
     * 5. ActorSplit: Actor工厂拆掉Actor
     */

    public void Dispose()
    {
        DisposeHelpers();
        DisposeAttributes();
        this._cacheGameLevelMgr = null;
    }


    public void InitByGameLevel(IGGameLevelMgr gameLevel)
    {
        this._cacheGameLevelMgr = gameLevel;
        this._ident = gameLevel.ActorFactory.GenActorIdent();
        InitAttributes();
        InitHelpers();
    }

    public void LogicUpdate(float detlatime)
    {
        LogicUpdateHelper(detlatime);
    }


    public void Tick()
    {
        TickHelper();
    }
    //

    private void TickHelper()
    {
        int count = this._listOfHelpers.Count;
        for (int i = 0;i<count;++i)
        {
            var helper = this._listOfHelpers[i];
            helper.Tick();
        }
    }
    private void LogicUpdateHelper(float detlatime)
    {
        int count = this._listOfHelpers.Count;
        for (int i = 0; i < count; ++i)
        {
            var helper = this._listOfHelpers[i];
            helper.LogicUpdate(detlatime);
        }
    }
    private void InitHelpers()
    {
        int count = this._listOfHelpers.Count;
        for (int i = 0; i < count; ++i)
        {
            var helper = this._listOfHelpers[i];
            helper.InitByActor(this);
        }
    }
    private void DisposeHelpers()
    {
        int count = this._listOfHelpers.Count;
        for (int i = 0; i < count; ++i)
        {
            var helper = this._listOfHelpers[i];
            helper.Dispose();
        }
        _dicOfHelpers.Clear();
    }


    private void InitAttributes()
    {
        int count = this._listOfAttributes.Count;
        for (int i = 0; i < count; ++i)
        {
            var attribute = this._listOfAttributes[i];
            attribute.InitByActor(this);
        }
    }
    private void DisposeAttributes() 
    {
        int count = this._listOfAttributes.Count;
        for (int i = 0; i < count; ++i)
        {
            var attribute = this._listOfAttributes[i];
            attribute.Dispose();
        }
        _dicOfAttributes.Clear();
    }
}